home *** CD-ROM | disk | FTP | other *** search
- # Source Generated with Decompyle++
- # File: in.pyc (Python 2.5)
-
- import os
- import sys
- import cgi
- import re
- import gobject
- import gtk
- import gnome
- import gnome.ui as gnome
- import gnomevfs
- import deskbar
- import deskbar.Handler as deskbar
- import deskbar.Utils as deskbar
- import deskbar.Match as deskbar
- from gettext import gettext as _
- from os.path import exists, dirname
- from deskbar.defs import VERSION
- from deskbar.Utils import is_program_in_path, spawn_async, url_show, url_show_file
- MAX_RESULTS = 20
-
- try:
- import beagle
- except:
- pass
-
-
- def _show_start_beagle_dialog(dialog):
- dialog = gtk.Dialog(_('Start Beagle Daemon?'), dialog, gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT)
- dialog.set_default_size(350, 150)
- dialog.add_button(gtk.STOCK_CANCEL, gtk.RESPONSE_REJECT)
- dialog.add_button(_('Start Beagle Daemon'), gtk.RESPONSE_ACCEPT)
- label = gtk.Label(_('The Beagle daemon does not appear to be running.\n You need to start it to use the Beagle Live handler.'))
- dialog.vbox.add(label)
- label.show()
- response = dialog.run()
- dialog.destroy()
- if response == gtk.RESPONSE_ACCEPT:
- print 'Starting Beagle Daemon.'
- if not spawn_async([
- 'beagled']):
- print >>sys.stfderr, "Failed to start beagled. Perhaps the beagle daemon isn't installed?"
- warn = gtk.MessageDialog(flags = gtk.DIALOG_MODAL, type = gtk.MESSAGE_WARNING, buttons = gtk.BUTTONS_CLOSE, message_format = _('Failed to start Beagle'))
- warn.format_secondary_text(_("Perhaps the beagle daemon isn't installed?"))
- warn.run()
- warn.destroy()
-
-
-
-
- def _check_requirements():
-
- try:
- import deskbar
- import beagle
- except Exception:
- e = None
- return (deskbar.Handler.HANDLER_IS_NOT_APPLICABLE, 'Could not load beagle, libbeagle has been compiled without python bindings:' + str(e), None)
-
- if not beagle.beagle_util_daemon_is_running():
- if is_program_in_path('beagled'):
- return (deskbar.Handler.HANDLER_HAS_REQUIREMENTS, 'Beagle daemon is not running.', _show_start_beagle_dialog)
- else:
- return (deskbar.Handler.HANDLER_IS_NOT_APPLICABLE, 'Beagled could not be found in your $PATH. Unable to start the beagled daemon', None)
- else:
- return (deskbar.Handler.HANDLER_IS_HAPPY, None, None)
-
- HANDLERS = {
- 'BeagleLiveHandler': {
- 'name': _('Beagle Live'),
- 'description': _('Search all of your documents (using Beagle), as you type'),
- 'requirements': _check_requirements,
- 'version': VERSION } }
- TYPES = {
- 'Contact': {
- 'name': ('fixme:FileAs',),
- 'action': 'evolution %(uri)s',
- 'icon': 'stock_contact',
- 'description': _('Edit contact %s') % '<b>%(name)s</b>',
- 'category': 'people' },
- 'MailMessage': {
- 'name': ('dc:title', 'parent:dc:title'),
- 'action': 'evolution %(uri)s',
- 'icon': 'stock_mail',
- 'extra': {
- 'sender': ('fixme:from_name', 'parent:fixme:from_name') },
- 'description': _('From %s') % '<i>%(sender)s</i>' + '\n<b>%(name)s</b>',
- 'category': 'emails' },
- 'File': {
- 'name': ('beagle:ExactFilename',),
- 'action': (lambda d: url_show_file('file://' + d['uri'])),
- 'icon': 'stock_new',
- 'description': _('Open %s') % '<b>%(name)s</b>',
- 'snippet': True,
- 'category': 'files' },
- 'FeedItem': {
- 'name': ('dc:title',),
- 'action': (lambda d: url_show(d['identifier'])),
- 'icon': 'stock_news',
- 'description': _('News from %s') % '<i>%(publisher)s</i>' + '\n<b>%(name)s</b>',
- 'snippet': True,
- 'category': 'news',
- 'extra': {
- 'publisher': ('dc:publisher',),
- 'identifier': ('dc:identifier',) } },
- 'Note': {
- 'name': ('dc:title',),
- 'action': 'tomboy --open-note %(uri)s',
- 'icon': 'stock_notes',
- 'description': _('Note: %s') % '<b>%(name)s</b>',
- 'snippet': True,
- 'category': 'notes' },
- 'IMLog': {
- 'name': ('fixme:speakingto',),
- 'extra': {
- 'client': ('fixme:client',) },
- 'action': "beagle-imlogviewer --client %(client)s --highlight-search '%(text)s' %(uri)s",
- 'icon': 'im',
- 'description': _('With %s') % '<b>%(name)s</b>',
- 'snippet': True,
- 'category': 'conversations' },
- 'Calendar': {
- 'name': ('fixme:summary',),
- 'action': 'evolution %(uri)s',
- 'icon': 'stock_calendar',
- 'description': _('Calendar: %s') % '<b>%(name)s</b>',
- 'category': 'documents' },
- 'WebHistory': {
- 'name': ('dc:title',),
- 'action': (lambda d: url_show_file(d['uri'])),
- 'icon': 'stock_bookmark',
- 'description': _('Open History Item %s') % '<i>%(name)s</i>' + '\n%(escaped_uri)s',
- 'category': 'web' } }
- for key, val in TYPES.items():
- if 'snippet' in val and val['snippet']:
- val['description'] += '%(snippet)s'
- continue
-
-
- class BeagleLiveMatch(deskbar.Match.Match):
-
- def __init__(self, handler, result = None, **args):
- '''
- \t\tresult: a dict containing:
- \t\t\t"name" : a name sensible to display for this match
- \t\t\t"uri": the uri of the match as provided by the beagled \'Uri: \'-field
- \t\t\t"type": One of the types listed in the TYPES dict
-
- \t\t-- and optionally extra fields as provided by the corresponding entry in TYPES.
- \t\tFx. "MailMessage". has an extra "sender" entry.
- \t\t'''
- deskbar.Match.Match.__init__(self, handler, name = result['name'], **args)
- self.result = result
- action = TYPES[self.result['type']]['action']
- if not callable(action) and action.startswith('beagle-imlogviewer'):
- self.result['uri'] = gnomevfs.get_local_path_from_uri(self.result['uri'])
-
- self._icon = None
- if result['type'] == 'File':
-
- try:
- self._icon = deskbar.Utils.load_icon_for_file(result['uri'])
- except Exception:
- pass
- except:
- None<EXCEPTION MATCH>Exception
-
-
- None<EXCEPTION MATCH>Exception
- if self._icon == None:
- self._icon = handler.ICONS[result['type']]
-
-
-
- def get_category(self):
-
- try:
- return TYPES[self.result['type']]['category']
- except:
- return 'default'
-
-
-
- def get_name(self, text = None):
- if text:
- self.result['text'] = text
- self.result['text'] = self.result['text'].replace("'", "\\'")
-
- return self.result
-
-
- def get_verb(self):
- return TYPES[self.result['type']]['description']
-
-
- def action(self, text = None):
- self.get_name(text)
- action = TYPES[self.result['type']]['action']
- if callable(action):
- action(self.result)
- else:
- action = action % self.result
- args = action.split(' ')
- print 'BeagleLive spawning:', action, args
- spawn_async(args)
-
-
- def get_hash(self, text = None):
- if 'uri' in self.result:
- return self.result['uri']
-
-
-
-
- class SnippetContainer:
-
- def __init__(self, hit):
- self.hit = hit
- self.snippet = None
-
-
-
- class BeagleLiveHandler(deskbar.Handler.SignallingHandler):
-
- def __init__(self):
- deskbar.Handler.SignallingHandler.__init__(self, ('system-search', 'best'))
- self.counter = { }
- self.snippets = { }
- self.set_delay(500)
-
-
- def initialize(self):
- self.beagle = beagle.Client()
- self.ICONS = self._BeagleLiveHandler__load_icons()
-
-
- def __load_icons(self):
- res = { }
- for t in TYPES.iterkeys():
- icon_file = TYPES[t]['icon']
- if not icon_file:
- continue
-
- res[t] = deskbar.Utils.load_icon(icon_file)
-
- return res
-
-
- def query(self, qstring):
- beagle_query = beagle.Query()
- beagle_query.add_text(qstring)
- beagle_query.connect('hits-added', self.hits_added, qstring, MAX_RESULTS)
-
- try:
- self.beagle.send_request_async(beagle_query)
- except:
- return None
-
- self.counter[qstring] = { }
-
-
- def _on_snippet_received(self, request, response, query, container, qstring, qmax):
- container.snippet = response.get_snippet()
- self._on_hit_added(query, container, qstring, qmax)
-
-
- def _on_snippet_closed(self, request, query, container, qstring, qmax):
- if container.snippet == None:
- self._on_hit_added(query, container, qstring, qmax)
-
- container.hit.unref()
-
-
- def _on_hit_added(self, query, hit, qstring, qmax):
- fire_signal = False
- snippet = None
- if hit.__class__ == SnippetContainer:
- hit = hit.hit
- snippet = hit.snippet
- fire_signal = True
-
- if hit.get_type() not in self.counter[qstring]:
- self.counter[qstring][hit.get_type()] = 0
-
- if self.counter[qstring][hit.get_type()] >= qmax:
- return None
-
- hit_type = TYPES[hit.get_type()]
- result = {
- 'uri': hit.get_uri(),
- 'type': hit.get_type() }
- name = None
- for prop in hit_type['name']:
-
- try:
- name = hit.get_properties(prop)[0]
- except:
-
- try:
- name = hit.get_property(prop)
-
-
- if name != None:
- result['name'] = name
- break
- continue
-
- if name == None:
- result['name'] = _('?')
-
- if 'extra' in hit_type:
- for prop, keys in hit_type['extra'].items():
- val = None
- for key in keys:
-
- try:
- val = hit.get_properties(key)[0]
- except:
-
- try:
- val = hit.get_property(key)
-
-
- if val != None:
- result[prop] = val
- break
- continue
-
- if val == None:
- result[prop] = _('?')
- continue
-
-
- for key, val in result.items():
- if key == 'uri' or key == 'identifier':
- result['escaped_' + key] = cgi.escape(val)
- continue
- result[key] = cgi.escape(val)
-
- if snippet != None:
- tmp = re.sub('<.*?>', '', snippet)
- tmp = re.sub('</.*?>', '', tmp)
- result['snippet'] = "\n<span foreground='grey' size='small'>%s</span>" % cgi.escape(tmp)
- else:
- result['snippet'] = ''
- self.counter[qstring][hit.get_type()] = self.counter[qstring][hit.get_type()] + 1
- match = BeagleLiveMatch(self, result)
- if fire_signal:
- self.emit_query_ready(qstring, [
- match])
- else:
- return match
-
-
- def hits_added(self, query, response, qstring, qmax):
- hit_matches = []
- for hit in response.get_hits():
- if hit.get_type() not in TYPES:
- print 'WARNING: Beagle live seen an unknown type:', hit.get_type()
- continue
-
- if 'snippet' in TYPES[hit.get_type()] and TYPES[hit.get_type()]['snippet']:
- req = beagle.SnippetRequest()
- req.set_query(query)
- req.set_hit(hit)
- container = SnippetContainer(hit)
- hit.ref()
- req.connect('response', self._on_snippet_received, query, container, qstring, qmax)
- req.connect('closed', self._on_snippet_closed, query, container, qstring, qmax)
- self.beagle.send_request_async(req)
- continue
-
- match = self._on_hit_added(query, hit, qstring, qmax)
- if match != None:
- hit_matches.append(match)
- continue
-
- self.emit_query_ready(qstring, hit_matches)
-
-
-